package com.example.trb.controller.education;

import com.example.trb.dto.ObjectDTO;
import com.example.trb.entity.Department;
import com.example.trb.entity.User;
import com.example.trb.service.education.EducationService;
import com.example.trb.util.result.CommonResult;
import com.example.trb.util.result.ReturnCode;
import com.example.trb.util.result.SysReturnCode;
import com.example.trb.vo.EducationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.ParseException;

@RestController
public class EducationController {
    @Autowired
    EducationService eduService;
    @Value("${session_user}")
    String userKey;
    @Value("${max_size_file_upload}")
    Integer maxSizeFileUpload;

    @PutMapping("/education")
    public CommonResult putEducation(@RequestBody @Validated EducationVO vo) {
        ObjectDTO dto = eduService.updateById(vo);
        ReturnCode returnCode = dto.getCode();
        return CommonResult.common(returnCode.code(), returnCode.message(), null);
    }



    @DeleteMapping("/education")
    public CommonResult deleteEducation(@RequestBody EducationVO vo) {
        ObjectDTO dto = eduService.removeById(vo.getId());
        ReturnCode returnCode = dto.getCode();
        return CommonResult.common(returnCode.code(), returnCode.message(), null);
    }

    @PostMapping("/education")
    public CommonResult addEducation(
            @Validated @RequestBody EducationVO vo,
            HttpSession session
    ) throws ParseException {
        if (vo.getMaterials().length > maxSizeFileUpload) {
            SysReturnCode returnCode = SysReturnCode.BAD_REQUEST;
            return CommonResult.fail(returnCode.code, "学历材料太多了");
        }

        User user = (User) session.getAttribute("user");
        ObjectDTO dto = eduService.save(vo, user.getId());
        ReturnCode code = dto.getCode();
        return CommonResult.common(code.code(), code.message(), dto.getData());
    }


    @PostMapping("/education/status")
    public CommonResult status(@RequestParam("id") Integer id,@RequestParam("status") String status)  {
        eduService.updateStatusById(id,status);
        return CommonResult.success(null);
    }



    @GetMapping("/education")
    public CommonResult getEducation(@RequestParam("id") Integer id) {
        ObjectDTO dto = eduService.getById(id);
        ReturnCode returnCode = dto.getCode();
        return CommonResult.common(returnCode.code(), returnCode.message(), dto.getData());
    }

    @GetMapping("/education/list")
    public CommonResult listEducation(HttpSession session) {
        User user = (User) session.getAttribute(userKey);
        ObjectDTO dto = eduService.listByUserId(user.getId());
        ReturnCode returnCode = dto.getCode();
        return CommonResult.common(returnCode.code(), returnCode.message(), dto.getData());
    }


}
